package com.linjiahao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.eduservice.entity.EduTeacher;
import com.linjiahao.eduservice.service.EduTeacherService;
import com.linjiahao.eduservice.vo.TeacherQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-15
 */
@Api("讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public ResultMap list(){
        return ResultMap.ok().data("item",teacherService.list(null));
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public ResultMap removeById( @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if(b){
            return ResultMap.ok();
        }else {
            return ResultMap.error();
        }
    }

    @ApiOperation(value = "分页讲师列表")
    @PostMapping("{page}/{limit}")
    public ResultMap pageList(@ApiParam(name = "page", value = "当前页码", required = true)
        @PathVariable Long page, @ApiParam(name = "limit", value = "每页记录数", required = true)
        @PathVariable Long limit,@ApiParam(name = "teacherQuery", value = "查询对象", required = false)
         @RequestBody TeacherQuery teacherQuery){
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  ResultMap.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping
    public ResultMap save(@ApiParam(name = "teacher", value = "讲师对象", required = true)
        @RequestBody EduTeacher teacher){
        teacherService.save(teacher);
        return ResultMap.ok();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public ResultMap getById(@ApiParam(name = "id", value = "讲师ID", required = true)
        @PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return ResultMap.ok().data("item", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public ResultMap updateById(@ApiParam(name = "id", value = "讲师ID", required = true)
        @PathVariable String id, @ApiParam(name = "teacher", value = "讲师对象", required = true)
        @RequestBody EduTeacher teacher){
        teacher.setId(id);
        teacherService.updateById(teacher);
        return ResultMap.ok();
    }
}

